﻿
using System;
using SqlSugar;
using System.Threading.Tasks;
using System.Linq.Expressions;

using Oliving.Edge.Model;
using Oliving.Edge.IRepository.UnitOfWork;
using Oliving.Edge.Repository.Base;
using Oliving.Edge.Model.Dto.XLT;
using Oliving.Edge.IRepository.XLT;
using Oliving.Edge.Model.Models.XLT;

namespace Oliving.Edge.Repository.XLT
{
    /// <summary>
    /// Base_EquipmentCategoryRepository
    /// </summary>
    public class Base_EquipmentCategoryRepository : BaseRepository<Base_EquipmentCategory>, IBase_EquipmentCategoryRepository
    {
        public Base_EquipmentCategoryRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
        {
        }






        /// <summary>
        /// 自定义分页查询
        /// </summary>
        /// <param name="whereExpression">查询条件</param>
        /// <param name="pageIndex" > 页码（下标0）</param>
        /// <param name="pageSize" > 页大小</param>
        /// <param name="sortField" > 排序字段</param>
        /// <param name="sortOrder" > 排序顺序（asc/desc）</param>
        /// <returns></returns>
        public async Task<PageModel<Base_EquipmentCategoryPageDto>> QueryPageList(
                Expression<Func<Base_EquipmentCategory, bool>> whereExpression,
                int pageIndex = 1, int pageSize = 20,
                string sortField = null, string sortOrder = null
                )
        {


            #region 排序

            string strOrderByFileds = null;

            if (!string.IsNullOrEmpty(sortField))
            {
                strOrderByFileds = sortField;
                if (sortOrder != null && sortOrder.ToLower().Contains("desc"))
                {
                    strOrderByFileds = strOrderByFileds + " desc";
                }
                else
                {
                    strOrderByFileds = strOrderByFileds + " asc";
                }
            }

            #endregion



            RefAsync<int> totalCount = 0;
            var list = await Db.Queryable<Base_EquipmentCategory, WY_Project>
               ((c, td) => new object[] { JoinType.Left, c.ProjectId == td.Id })
             //.OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
             .WhereIF(whereExpression != null, whereExpression)
                //.GroupBy((c, td) => td.TD_Type)
                //.GroupBy((c, td) => Convert.ToDateTime(c.Canning_BeginTime).ToShortDateString())
                .Select<Base_EquipmentCategoryPageDto>()
                .ToPageListAsync(pageIndex, pageSize, totalCount);

            int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / pageSize.ObjToDecimal())).ObjToInt();
            return new PageModel<Base_EquipmentCategoryPageDto>()
            {
                dataCount = totalCount,
                pageCount = pageCount,
                page = pageIndex,
                PageSize = pageSize,
                data = list
            };

        }







    }
}

